package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.dto.CourseWithUnreadCountDTO;
import com.production.CoursePlatform.entity.StudentNotice;
import com.production.CoursePlatform.service.StudentNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/studentNotice")
public class StudentNoticeController {

    @Autowired
    private StudentNoticeService studentNoticeService;

    // 获取某课程下未读学生数量
    @GetMapping("/number/{cnID}")
    public ResponseEntity<Integer> getStudentNumber(@PathVariable int cnID) {
        int studentNumber = studentNoticeService.getStudentNumberOfCtID(cnID);
        return ResponseEntity.ok(studentNumber);
    }

    // 更新学生通知状态
    @PutMapping("/update/{snID}")
    public ResponseEntity<String> updateStudying(@PathVariable int snID) {
        int updatedCount = studentNoticeService.updateStudying(snID);
        if (updatedCount > 0) {
            return ResponseEntity.ok("Successfully updated studying status.");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("No matching record found.");
        }
    }

    // 查找未读学生的学号列表
    @GetMapping("/unread/{cnID}")
    public ResponseEntity<List<StudentNotice>> findUnreadInCtID(@PathVariable int cnID) {
        List<StudentNotice> unreadNotices = studentNoticeService.findUnreadInCtID(cnID);
        if (unreadNotices.isEmpty()) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Collections.emptyList());
        }
        return ResponseEntity.ok(unreadNotices);
    }

    // 获取某学生所有课程未读消息数
    @GetMapping("/course/{sID}")
    public ResponseEntity<List<CourseWithUnreadCountDTO>> findCoursesWithUnreadNotices(@PathVariable int sID) {
        List<CourseWithUnreadCountDTO> courses = studentNoticeService.findCoursesWithUnreadNoticesBySID(sID);
        return ResponseEntity.ok(courses);
    }

    // 获取某学生所有课程未读消息数之和
    @GetMapping("/messageNum/{sID}")
    public ResponseEntity<Integer> findTotalUnreadMessages(@PathVariable("sID") int sID) {
        int totalUnreadMessages = studentNoticeService.findTotalUnreadMessagesBySID(sID);
        return ResponseEntity.ok(totalUnreadMessages);
    }
}
